/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controlador;


import Controlador.Manejadores.NuevoExportador;
import Controlador.Manejadores.NuevoImportador;
import Controlador.Manejadores.NuevoSanitario;
import Controlador.Manejadores.NuevoTransporte;
import Controlador.Manejadores.MostrarNuevoSanitario;
import Controlador.Manejadores.NuevaBoletaSenasa;
import Persistencia.DAOFactory;
import Persistencia.Entidades.Responsable;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author zeta
 */
public class ControladorServlet extends HttpServlet {

    private Map handlerHash = new HashMap();
    private DAOFactory dao = DAOFactory.getDAOFactory(DAOFactory.ORACLE);

    @Override
    public void init() throws ServletException {



        handlerHash.put("/nuevoExportador.html", new NuevoExportador(dao));
        handlerHash.put("/nuevoImportador.html", new NuevoImportador(dao));
        handlerHash.put("/nuevoTransporte.html", new NuevoTransporte(dao));
        handlerHash.put("/nuevoSanitario.html", new NuevoSanitario(dao));
        handlerHash.put("/mostrarNuevoSanitario.html", new MostrarNuevoSanitario(dao));
        handlerHash.put("/nuevoSanitario.html", new NuevoSanitario(dao));
        handlerHash.put("/nuevaBoletaSenasa.html", new NuevaBoletaSenasa(dao));
        /*handlerHash.put("/verEmpresas.html", new com.sgt.mvc.app.VerEmpresas());
        handlerHash.put("/nuevaEmpresa.html", new com.sgt.mvc.app.NuevaEmpresa());
        handlerHash.put("/eliminarEmpresa.html", new com.sgt.mvc.app.eliminarEmpresa());
         */
    }

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //HttpSession session = request.getSession();
        //Responsable usu = (Responsable) session.getAttribute("usuario");

        //if (usu == null) {
          //  request.getRequestDispatcher("/login.jsp").forward(request, response);
        //}


        System.out.println("Controlador: servlet path is " + request.getServletPath());
        RequestHandler rh = (RequestHandler) handlerHash.get(request.getServletPath());
        if (rh == null) {
          
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        } else {
            String viewURL = rh.handleRequest(request, response);
            if (viewURL == null) {
            } else {
                request.getRequestDispatcher(viewURL).forward(request, response);
            }
        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);

    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
